<template>
  <div class="content">
    <BreadNav
      :location="location"
      :routers="routers"
      @getData="getData"
    />
    <main>
      <article>
        <div v-if="!showDetail">
          <NewBlog
            :articles="newArticles"
            :isIndex="false"
            @getData="getData"
          />
        </div>
        <ArticleContent v-if="showDetail" />
      </article>
      <aside>
        <AsideLinks />
      </aside>
    </main>
    <Footer />
  </div>
</template>

<script>
import BreadNav from '@/components/BreadNav.vue'
import ArticleContent from '@/components/ArticleContent.vue'
import AsideLinks from '@/components/AsideLinks.vue'
import NewBlog from '@/components/NewBlog.vue'
import Footer from '@/components/Footer.vue'
import { getArticleList } from '../api/article'
import bus from '@/main'
export default {
  components: {
    BreadNav,
    Footer,
    ArticleContent,
    AsideLinks,
    NewBlog
  },
  data() {
    return {
      location: '前端分享',
      routers: 'qianduan',
      newArticles: [],
      showDetail: false,
      imgs: [],
      articleQ: {}
    }
  },
  created() {
    this.getData()
  },
  mounted() {
    bus.$on('getData', this.getData)
  },
  methods: {
    async getData() {
      if (this.$route.query.id) {
        this.showDetail = true
        // await getArticle({ id: this.$route.query.id }).then((res) => {
        //   this.articleQ = res.data.data;
        //   // 替换v-html中的图片属性img设置样式
        //   // this.articleQ.content = this.articleQ.content.replace(
        //   //   /<img/g,
        //   //   `<img style='max-width:80%;height:auto;margin:5px' `
        //   // );
        //   this.imgs = this.articleQ.image && this.articleQ.image.split(",");
        //   console.log(this.imgs);
        // });
      } else {
        this.showDetail = false
        const opt = {
          page: 1,
          per_page: 100,
          channel_id: 5
        }
        const res = await getArticleList(opt)
        this.newArticles = [...res.data.data]
        console.log(this.newArticles)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
// 宽度大于1201px时
@media screen and (min-width: 1201px) {
  .content {
    width: 1200px;
    margin: 0 auto;
  }
}

// 宽度小于1200px时
@media screen and (max-width: 1200px) {
  .content {
    margin: 0 40px;
  }
}

@media only screen and (max-width: 768px) {
  body .content main article {
    width: 100%;
  }
  aside {
    display: none;
  }
}

@media only screen and (max-width: 480px) {
  .content {
    margin: 0 15px;
  }
}

.content {
  main {
    display: flex;
    article {
      width: 860px;
    }
    aside {
      width: 320px;
      margin-left: 20px;
      height: 300px;
      border-radius: 3px;
    }
  }
}
</style>
